<template>
  <div class="">
    <!-- <van-icon color="#777" name="good-job-o" /> -->
    <van-icon
      :name="value === 1 ? 'good-job' : 'good-job-o'"
      :color="value === 1 ? '#ffa500' : ''"
      :class="{
        liked: value === 1,
      }"
      :loading="loading"
      @click="onCollect"
    />
  </div>
</template>
<script>
import { likeArt, cancelLiketArt } from '@/api/article';
export default {
  name: 'LikeArt',
  components: {},
  props: {
    value: {
      type: Number,
      require: true
    },
    artId: {
      type: [Number, Object, String],
      require: true
    }
  },
  data () {
    return {
      loading: false
    };
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onCollect () {
      this.loading = true;
      try {
        let status = -1;
        if (this.value !== 1) {
          await likeArt(this.artId);
          status = 1;
        } else if (this.value === 1) {
          await cancelLiketArt(this.artId);
        }
        this.$emit('input', status);
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞');
      } catch (error) {
        this.$toast('点赞失败，请稍后重试');
      }
      this.loading = false;
    }
  }
};
</script>
<style scoped lang='less'>
.liked {
  .van-icon {
    color: #e5645f;
  }
}
</style>
